package com.lyj.service.impl;

import com.lyj.mapper.NurseMapper;
import com.lyj.pojo.*;
import com.lyj.service.NurseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class NurseServiceImpl implements NurseService {

    @Autowired
    NurseMapper nurseMapper;


    @Override
    public Nurse login(String username, String password, String duty) {
        Nurse login = nurseMapper.login(username, password, duty);
        return login;
    }

    @Override
    public Boolean updateNurseById(Nurse nurse) {
        Boolean aBoolean = nurseMapper.updateNurseById(nurse);
        return aBoolean;
    }

    @Override
    public List<OldMan> queryOldManByNurseId(int nurseId) {
        List<OldMan> oldManList = nurseMapper.queryOldManByNurseId(nurseId);
        return oldManList;
    }

    @Override
    public List<NurseSalary> querySalaryByNurseId(int nurseId) {
        List<NurseSalary> nurseList = nurseMapper.querySalaryByNurseId(nurseId);
        return nurseList;
    }

    @Override
    public Boolean vacation(Vacation vacation) {
        Boolean aBoolean = nurseMapper.vacation(vacation);
        return aBoolean;
    }

    @Override
    public List<Vacation> queryAllByNurseId(int nurseId) {
        List<Vacation> vacationList = nurseMapper.queryAllByNurseId(nurseId);
        return vacationList;
    }

    //修改请假信息（同意，拒绝，审批中）
    @Override
    public Boolean updateVacation(Vacation vacation) {

        Boolean aBoolean = nurseMapper.updateVacation(vacation);
        String duty = vacation.getRemarks();
        if ("同意".equals(duty)){
//如果同意，根据护工id查出，他所管理的20个老人的id。
            List<OldMan> oldMEN = nurseMapper.queryOldManByNurseId(vacation.getNurseId());
            System.out.println(oldMEN);
            for (OldMan oldMan : oldMEN) {
                System.out.println("------------------"+oldMan);
                //根据oldmanId,//查询老人的备用护工（评价高的护工）
                Satisfaction satisfaction = nurseMapper.querySatisNurseByoldId(oldMan.getOldId());
                //循环查找没请假的，
                System.out.println("==满意的评价好的护工satisfaction=="+satisfaction);//Satisfaction(satisId=4, oldId=4, nurse1=5, nurse2=6, nurse3=4, nurse4=2, nurse5=1)
                //循环查找五个备用护工没请假的
                //查询第一个护工，判断是否请假
                System.out.println("---nurse1--id--"+satisfaction.getNurse1());
                Vacation vacation1 = nurseMapper.queryNoVacationNurse(satisfaction.getNurse1());
                //判断一下表中是否有这个人的请假信息，再判断这个人的请假或者批准
                System.out.println("vacation1==="+vacation1);
                System.out.println("护工nurseid==="+satisfaction.getNurse1());
                //nurse非空，证明请假批准了，然后看下一个人是否请假
                if (vacation1!=null) {//nrese1请假了
                    System.out.println("==vacation1==非null");
                    Vacation vacation2 = nurseMapper.queryNoVacationNurse(satisfaction.getNurse2());
                    if (vacation2!=null){//nrese2请假了
                        System.out.println("==vacation2==非null");
                        Vacation vacation3 = nurseMapper.queryNoVacationNurse(satisfaction.getNurse3());
                        if (vacation3!=null){//nrese3请假了
                            System.out.println("==vacation3==非null");
                            Vacation vacation4 = nurseMapper.queryNoVacationNurse(satisfaction.getNurse4());
                            if (vacation4!=null){//nrese4请假了
                                System.out.println("==vacation4==非null");
                                Vacation vacation5 = nurseMapper.queryNoVacationNurse(satisfaction.getNurse5());
                                if (vacation5!=null){//nrese5请假了
                                    System.out.println("==vacation5==非null");
                                    Boolean aBoolean6 = nurseMapper.updateOldManNurseId(oldMan.getOldId(), 0,"");
                                    continue;

                                }else {
                                    Nurse nurse = nurseMapper.queryNurseById(satisfaction.getNurse5());
                                    System.out.println("==null==oldId:"+oldMan.getOldId()+"==nurseId=="+satisfaction.getNurse5()+"==username=="+nurse.getUsername());
                                    Boolean aBoolean5 = nurseMapper.updateOldManNurseId(oldMan.getOldId(), satisfaction.getNurse5(),nurse.getUsername());
                                    //return aBoolean5;
                                }
                            }else {
                                Nurse nurse = nurseMapper.queryNurseById(satisfaction.getNurse4());
                                System.out.println("==null==oldId:"+oldMan.getOldId()+"==nurseId=="+satisfaction.getNurse4()+"==username=="+nurse.getUsername());
                                Boolean aBoolean4 = nurseMapper.updateOldManNurseId(oldMan.getOldId(), satisfaction.getNurse4(),nurse.getUsername());
                                //return aBoolean4;
                            }
                        }else {
                            Nurse nurse = nurseMapper.queryNurseById(satisfaction.getNurse3());
                            System.out.println("==null==oldId:"+oldMan.getOldId()+"==nurseId=="+satisfaction.getNurse3()+"==username=="+nurse.getUsername());
                            Boolean aBoolean3 = nurseMapper.updateOldManNurseId(oldMan.getOldId(), satisfaction.getNurse3(),nurse.getUsername());
                            //return aBoolean3;
                        }
                    }else {
                        Nurse nurse = nurseMapper.queryNurseById(satisfaction.getNurse2());
                        System.out.println("==null==oldId:"+oldMan.getOldId()+"==nurseId=="+satisfaction.getNurse2()+"==username=="+nurse.getUsername());
                        Boolean aBoolean2 = nurseMapper.updateOldManNurseId(oldMan.getOldId(), satisfaction.getNurse2(),nurse.getUsername());
                        //return aBoolean2;
                    }
                } else {
                    //查询改护工姓名
                    Nurse nurse = nurseMapper.queryNurseById(satisfaction.getNurse1());
                    System.out.println("==null==oldId:"+oldMan.getOldId()+"==nurseId=="+satisfaction.getNurse1()+"==username=="+nurse.getUsername());
                    //nurse空，查不到他的请假信息，那就是没有请假。将老人护工替换成这个nurseId。
                    Boolean aBoolean1 = nurseMapper.updateOldManNurseId(oldMan.getOldId(), satisfaction.getNurse1(),nurse.getUsername());
                    //return aBoolean1;

                }
            }

        }

        return aBoolean;
    }

    @Override
    public List<OldMan> queryOldManNurseNull() {

        List<OldMan> oldManNullList = nurseMapper.queryOldManNurseNull();
        return oldManNullList;
    }

}
